﻿using NB.MGJ.Common;
using NB.MGJ.FrameWork;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Service
{
    public class DBHService
    {
        public BaseRepository _baseRepository = new BaseRepository();

        #region 表是否存在
        /// <summary>
        /// 检查表是否存在 nbsecuritymain
        /// </summary>
        /// <param name="Table">表名</param>
        /// <returns></returns>
        public bool TabExists(string Table)
        {
            // string sql = string.Format("SHOW TABLES LIKE '%{0}%' ", Table);
            bool isExists = true;
            string sql = string.Format("SHOW TABLES LIKE '{0}'", Table);
            object obj = _baseRepository.GetSingle(sql);
            if ((string.Equals(obj, null)) || (string.Equals(obj, System.DBNull.Value)))
            {
                return false;
            }
            return isExists;
        }

        /// <summary>
        /// 判断表是否存在
        /// </summary>
        /// <param name="databaseName">数据库名称</param>
        /// <param name="Table">表名</param>
        /// <returns></returns>
        public bool TabExists(string databaseName, string Table)
        {
            // string sql = string.Format("SHOW TABLES LIKE '%{0}%' ", Table);
            bool isExists = true;
            string sql = string.Format("select table_name from information_schema.tables where table_schema='{0}' and table_type='base table' and table_Name LIKE '{1}';", databaseName, Table);
            object obj = _baseRepository.GetSingle(sql);
            if ((string.Equals(obj, null)) || (string.Equals(obj, System.DBNull.Value)))
            {
                return false;
            }
            return isExists;
        }

        /// <summary>
        /// 检查表是否存在 生码数据库
        /// </summary>
        /// <param name="Table"></param>
        /// <returns></returns>
        public bool TabExists_Code(string Table)
        {
            // string sql = string.Format("SHOW TABLES LIKE '%{0}%' ", Table);
            bool isExists = true;
            string sql = string.Format("use nbcodemain;SHOW TABLES LIKE '{0}'", Table);
            object obj = _baseRepository.GetSingle(sql);
            if ((object.Equals(obj, null)) || (object.Equals(obj, System.DBNull.Value)))
            {
                return false;
            }
            return isExists;

        }
        #endregion


    }



}

